package online.heycm.rbac.service.basic.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import online.heycm.platform.common.entity.result.Optional;
import online.heycm.platform.common.entity.result.PageResult;
import online.heycm.rbac.common.cache.ParamCfgCache;
import online.heycm.rbac.common.constant.ErrorCode;
import online.heycm.rbac.common.converter.ConvertHelper;
import online.heycm.rbac.common.utils.PageHelper;
import online.heycm.rbac.common.converter.RbacConverter;
import online.heycm.rbac.domain.basic.repository.ParamCfgRepository;
import online.heycm.rbac.domain.basic.repository.entity.BasicParamCfg;
import online.heycm.rbac.param.basic.ParamCfgAddReq;
import online.heycm.rbac.param.basic.ParamCfgEditReq;
import online.heycm.rbac.param.basic.ParamCfgPageReq;
import online.heycm.rbac.param.basic.ParamCfgRes;
import online.heycm.rbac.service.basic.ParamCfgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author hey
 * @version 1.0
 * @date 2024/1/12 15:37
 */
@Service
public class ParamCfgServiceImpl implements ParamCfgService {

    @Autowired
    private ParamCfgRepository paramCfgRepository;

    @Autowired
    private ParamCfgCache paramCfgCache;

    @Override
    public Optional addParamCfg(ParamCfgAddReq req) {
        BasicParamCfg entity = RbacConverter.INST.convert(req);
        boolean save = paramCfgRepository.save(entity);
        if (save) {
            paramCfgCache.update(entity);
        }
        return Optional.ifFail(save, ErrorCode.PARAM_INSERT);
    }

    @Override
    public Optional editParamCfg(ParamCfgEditReq req) {
        BasicParamCfg entity = RbacConverter.INST.convert(req);
        boolean save = paramCfgRepository.updateById(entity);
        if (save) {
            paramCfgCache.update(entity);
        }
        return Optional.ifFail(save, ErrorCode.PARAM_UPDATE);
    }

    @Override
    public Optional<ParamCfgRes> queryDetail(Integer paramId) {
        BasicParamCfg entity = paramCfgRepository.getById(paramId);
        if (entity == null) {
            return Optional.error(ErrorCode.PARAM_NOT_EXIST);
        }
        return Optional.of(RbacConverter.INST.convert(entity));
    }

    @Override
    public Optional<PageResult<ParamCfgRes>> pageParamCfg(ParamCfgPageReq req) {
        IPage<BasicParamCfg> page = PageHelper.getPage(req);
        LambdaQueryWrapper<BasicParamCfg> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(req.getParamPrefix()), BasicParamCfg::getParamPrefix, req.getParamPrefix());
        wrapper.eq(StringUtils.hasText(req.getParamName()), BasicParamCfg::getParamName, req.getParamName());
        wrapper.eq(StringUtils.hasText(req.getParamValue()), BasicParamCfg::getParamValue, req.getParamValue());
        wrapper.eq(StringUtils.hasText(req.getRemark()), BasicParamCfg::getRemark, req.getRemark());
        paramCfgRepository.page(page, wrapper);
        PageResult<ParamCfgRes> pageResult = ConvertHelper.page(page, RbacConverter.INST::convert);
        return Optional.of(pageResult);
    }
}
